package com.team.party.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.team.party.util.PageConstant.*;

import com.team.party.bean.Activity;
import com.team.party.bean.Manager;
import com.team.party.bean.Society;
import com.team.party.bean.Student;
import com.team.party.service.ManagerService;
import com.team.party.service.StudentService;
import com.team.party.service.impl.StudentServiceImpl;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentservice;
    @Autowired
    private ManagerService managerservice;
    

    /**
     * @Description:进入登录页面
     * @param:
     * @return:String
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return  LOGIN;
    }


    /**
     * @Description:进入注册页面
     * @return:String
     */
    @RequestMapping("/toRegist")
    public String insert() {
        return REGIST;
    }

    
    /**
     * @Description:学生注册
	 * @param stu_name 姓名
	 * @param stu_sex 性别
	 * @param stu_age 年龄
	 * @param stu_no 学生号
	 * @param stu_username 账号
	 * @param stu_password 密码
	 * @param stu_tel 电话
	 * @param stu_form 年级
	 * @param stu_scho 学院
	 * @return String
     * @throws Exception 
	 */
    @RequestMapping("/studentRegist")
    public Boolean regist(
			@RequestParam(value = "stu_name", required = false) String stu_name,
			@RequestParam(value = "stu_sex", required = false) String stu_sex,
			@RequestParam(value = "stu_age", required = false) int stu_age,
			@RequestParam(value = "stu_no", required = false) String stu_no,
			@RequestParam(value = "stu_username", required = false) String stu_username,
			@RequestParam(value = "stu_password", required = false) String stu_password,
			@RequestParam(value = "stu_tel", required = false) String stu_tel,
			@RequestParam(value = "stu_form", required = false) String stu_form,
			@RequestParam(value = "stu_scho", required = false) String stu_scho) throws Exception{
		
		return studentservice.register(stu_name, stu_sex, stu_age, stu_no, stu_username,
					stu_password, stu_tel, stu_form, stu_scho);
    }
    
    
    /**
     * @Description:用户注销
	 * @return String
	 */
    @RequestMapping("/logout")
    public String logout() {
    	
    	return null;
    }
    
    
    /**
     * @Description:通过学号获取学生信息
     * @param: stu_no 学号
     * @return:Student
     * @throws Exception 
     */
    @RequestMapping("/serchMessage")
    public Student serchMessage(
    		@RequestParam(value = "stu_no", required = false) String stu_no) throws Exception {

    	return studentservice.serchMessage(stu_no);
    }
    
    
    /**
     * @Description:通过学号修改学生信息
     * @Description:允许修改：姓名、性别、年龄、电话、年级、学院
	 * @param stu_name 姓名
	 * @param stu_sex 性别
	 * @param stu_age 年龄
	 * @param stu_tel 电话
	 * @param stu_form 年级
	 * @param stu_scho 学院
	 * @param stu_no 学号
     * @return:String
     * @throws Exception 
     */
    @RequestMapping("/changeMessage")
    public Boolean changeMessage(
    		@RequestParam(value = "stu_name", required = false) String stu_name, 
    		@RequestParam(value = "stu_sex", required = false) String stu_sex, 
    		@RequestParam(value = "stu_age", required = false) int stu_age, 
    		@RequestParam(value = "stu_tel", required = false) String stu_tel, 
    		@RequestParam(value = "stu_form", required = false) String stu_form,
    		@RequestParam(value = "stu_scho", required = false) String stu_scho, 
    		@RequestParam(value = "stu_no", required = false) String stu_no) throws Exception{	
    	
    	return studentservice.changeMessage(stu_name, stu_sex, stu_age, 
    			stu_tel, stu_form, stu_scho, stu_no);
    	
    }
    
    
    /**
     * @Description:通过学号修改密码
	 * @param stu_no 学号
	 * @param stu_password 密码
     * @return:String
     * @throws Exception 
     * 
     */
    @RequestMapping("/changePassword")
    public Boolean changePassword(
    		@RequestParam(value = "stu_no", required = false) String stu_no,
    		@RequestParam(value = "stu_password", required = false) String stu_password) throws Exception{	
    	
    	return studentservice.changePassword(stu_no, stu_password);
    }
    
    
    /**
     * 查看所有社团
     * @return:List<Society>
     * @throws Exception 
     */
    @RequestMapping("/serchSociety")
    public List<Society> serchSociety() throws Exception{
    	
    	return studentservice.serchSociety();
    }
    
    
    /**
     * 加入社团
	 * @param stu_no 学号
	 * @param soc_no 社团号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/joinSociety")
    public Boolean joinSociety(
    		@RequestParam(value = "stu_no", required = false) String stu_no,
    		@RequestParam(value = "soc_no", required = false) String soc_no,
    		HttpSession httpSession) throws Exception {
    	
    	Society society = managerservice.serchMySociety(soc_no);
    	
    	society.setSoc_count(society.getSoc_count() + 1);
    	Boolean n = studentservice.joinSociety(stu_no, soc_no);
    	if(n) {
    		return managerservice.changeSocietyMessage(society.getSoc_unit(), society.getSoc_locat(), society.getSoc_man(), society.getSoc_tele(), society.getSoc_count(), society.getSoc_intro(), society.getSoc_no());
        	
    	} else {
    		return false;
    	}
    }
    
    /**
     * 查看该学生加入的社团
     * @param stu_no 学号
     * @return:List<Society>
     * @throws Exception 
     */
    @RequestMapping("/serchJoinedSociety")
    public List<Society> serchJoinedSociety(
    		
    		@RequestParam(value = "stu_no", required = false) String stu_no) throws Exception{
    	return studentservice.serchJoinedSociety(stu_no);
    }
    
    
    /**
     * 退出社团
     * @param stu_no 学号
     * @param soc_no 退出的社团号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/exitSociety")
    public Boolean exitSociety(
    		@RequestParam(value = "stu_no", required = false) String stu_no,
    		@RequestParam(value = "soc_no", required = false) String soc_no ,
    		HttpSession httpSession) throws Exception {
    	
    	Society society = managerservice.serchMySociety(soc_no);
    	
    	society.setSoc_count(society.getSoc_count() - 1);
    	Boolean n = studentservice.exitSociety(stu_no, soc_no);
    	if(n) {
    		return managerservice.changeSocietyMessage(society.getSoc_unit(), society.getSoc_locat(), society.getSoc_man(), society.getSoc_tele(), society.getSoc_count(), society.getSoc_intro(), society.getSoc_no());
    	} else {
    		return false;
    	} 	
    }
    
    
    /**
     * 查找所有活动
     * @return:List<Activity>
     * @throws Exception 
     */
    @RequestMapping("/serchActivity")
    public List<Activity> serchActivity() throws Exception{
    	return studentservice.serchActivity();
    }
    
    
    /**
     * 加入活动
     * @param stu_no 学号
     * @param act_no 活动号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/joinActivity")
    public Boolean joinActivity(
    		@RequestParam(value = "stu_no", required = false) String stu_no,
    		@RequestParam(value = "act_no", required = false) String act_no) throws Exception {
    	
    	return studentservice.joinActivity(stu_no, act_no);
    }
    
    
    /**
     * 查询加入的活动
     * @param stu_no 学号
     * @return:List<Activity>
     * @throws Exception 
     */
    @RequestMapping("/serchJoinedActivity")
    public List<Activity> serchJoinedActivity(
    		@RequestParam(value = "stu_no", required = false) String stu_no) throws Exception{
    	
    	return studentservice.serchJoinedActivity(stu_no);
    }
    
    
    /**
     * 退出加入的活动
     * @param stu_no 学号
     * @param act_no 活动号
     * @return:Boolean
     * @throws Exception 
     */
    @RequestMapping("/exitActivity")
    public Boolean exitActivity(
    		@RequestParam(value = "stu_no", required = false) String stu_no,
    		@RequestParam(value = "act_no", required = false) String act_no) throws Exception {
    	
    	return studentservice.exitActivity(stu_no, act_no);
    }
    
    /**
     * 通过社团号、社团名模糊查询社团
     * @param soc_no 社团号
     * @param soc_name 社团名
     * @return List<Society>
     * @throws Exception
     */
    @RequestMapping("/serchSocietyByFuzzy")
    public List<Society> serchSocietyByFuzzy(
    		@RequestParam(value = "soc_no", required = false) String soc_no, 
    		@RequestParam(value = "soc_name", required = false) String soc_name) throws Exception {
    	
    	return studentservice.serchSocietyByFuzzy(soc_no, soc_name);
    }
    
    /**
     * 通过活动号、活动名模糊查询活动
     * @param act_no 活动号
     * @param act_name 活动名
     * @return List<Activity>
     * @throws Exception
     */
    @RequestMapping("/serchActivityByFuzzy")
    public List<Activity> serchActivityByFuzzy(
    		@RequestParam(value = "act_no", required = false) String act_no, 
    		@RequestParam(value = "act_name", required = false) String act_name) throws Exception {
    	
    	return studentservice.serchActivityByFuzzy(act_no, act_name);
    }
}